home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 09 - QuickDraw 3D / TextureMap / Polygon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-26  |  4.4 KB  |  184 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*          POLYGON.C            */
  3. /****************************/
  4.  
  5.  
  6. /****************************/
  7. /*    EXTERNALS             */
  8. /****************************/
  9.  
  10. #include <qd3d.h>
  11. #include <QD3DGeometry.h>
  12. #include <QD3DSet.h>
  13. #include <QD3DView.h>
  14. #include <QD3DShader.h>
  15. #include <QD3DGroup.h>
  16. #include <QD3DRenderer.h>
  17. #include <QD3DAcceleration.h>
  18. #include <QD3DDrawContext.h>
  19.  
  20. extern    TQ3ViewObject        gMyViewObject;
  21. extern    TQ3StyleObject        gMyStyleObject_Interpolation;
  22. extern    TQ3StyleObject        gMyStyleObject_Backfacing;
  23. extern    TQ3StyleObject        gMyStyleObject_Fillstyle;
  24. extern    TQ3ShaderObject        gMyShaderObject_IlluminationShader;
  25. extern    TQ3ShaderObject        gMyTextureShaderObject;
  26.  
  27.  
  28.  
  29. /****************************/
  30. /*    PROTOTYPES            */
  31. /****************************/
  32.  
  33. extern    void DoFatalAlert(Str255 s);
  34.  
  35. void DrawMyObjects(void);
  36. void MakeTexturedPolygonGroup(void);
  37.  
  38.  
  39. /****************************/
  40. /*    CONSTANTS             */
  41. /****************************/
  42.  
  43.  
  44. /*********************/
  45. /*    VARIABLES      */
  46. /*********************/
  47.  
  48. TQ3GroupObject        gMyPolygonGroup;
  49.  
  50.  
  51. /***************** MAKE TEXTURED POLYGON GROUP *****************/
  52. //
  53. // Creates a simple 4-sided polygon object which has
  54. // u/v attributes for the 4 vertices.  The polygon and
  55. // the texture shader object are placed into a new group object.
  56. //
  57.  
  58. void MakeTexturedPolygonGroup(void)
  59. {
  60. long                i;
  61. TQ3GeometryObject    polygonObj;
  62. TQ3GroupPosition    myGroupPosition;
  63. TQ3PolygonData        polygonData;
  64. TQ3Vertex3D            vertices[4] = {-20,0,20,nil,
  65.                                     20,0,20,nil,
  66.                                     20,0,-20,nil,
  67.                                     -20,0,-20,nil};
  68. TQ3AttributeSet        attribs[4];
  69. float                ambient = 1.0;
  70. TQ3Param2D            uv[4] = {0,0,1,0,1,1,0,1};
  71.  
  72.  
  73.                 /* CREATE A GROUP */
  74.     
  75.     gMyPolygonGroup = Q3OrderedDisplayGroup_New();
  76.     if (gMyPolygonGroup == nil)
  77.         DoFatalAlert("\pCouldnt make the group!");
  78.  
  79.  
  80.             /* ADD TEXTURE SHADER OBJECT TO GROUP */
  81.             
  82.     myGroupPosition = Q3Group_AddObject(gMyPolygonGroup, gMyTextureShaderObject);
  83.     if ( myGroupPosition == nil )
  84.         DoFatalAlert("\pQ3Group_AddObject failed!");
  85.  
  86.  
  87.             /* CREATE VERTICES OF POLYGON & SET UV'S */
  88.  
  89.     for (i=0; i < 4;i++)
  90.     {
  91.         attribs[i] = Q3AttributeSet_New();                    // make new attribute set
  92.         Q3AttributeSet_Add(attribs[i],                         // add uv attribute
  93.                     kQ3AttributeTypeShadingUV, &uv[i]);
  94.         vertices[i].attributeSet = attribs[i];
  95.     }
  96.  
  97.             /* CREATE NEW POLYGON OBJECT */
  98.  
  99.     polygonData.numVertices = 4;                            // 4 sided polygon
  100.     polygonData.vertices = vertices;                        // point to our array of vertices
  101.     polygonData.polygonAttributeSet = nil;                    // polygon itself has no attribs
  102.     polygonObj = Q3Polygon_New(&polygonData);                // create it
  103.     
  104.     
  105.         /* DISPOSE OF OTHER REFERENCES TO ATTRIBUTES */
  106.                 
  107.     for (i=0; i < 4; i++)
  108.         Q3Object_Dispose(attribs[i]);
  109.  
  110.                 /* ADD POLYGON INTO GROUP */
  111.                 
  112.     myGroupPosition = Q3Group_AddObject(gMyPolygonGroup, polygonObj);
  113.     if ( myGroupPosition == nil )
  114.         DoFatalAlert("\pQ3Group_AddObject failed!");    
  115.  
  116.             /* DISPOSE OF REFERENCE TO POLYGON */
  117.                     
  118.     Q3Object_Dispose(polygonObj);
  119. }
  120.  
  121.  
  122.  
  123. /******************* DRAW MY OBJECTS *********************/
  124. //
  125. // Draws a frame of animation for the FlyThru window.
  126. //
  127.  
  128. void DrawMyObjects(void)
  129. {
  130. TQ3Status                myStatus;
  131. TQ3ViewStatus            myViewStatus;
  132.  
  133.                 /* START RENDERING */
  134.                                 
  135.     myStatus = Q3View_StartRendering(gMyViewObject);            // start rendering
  136.     if ( myStatus == kQ3Failure )
  137.         DoFatalAlert("\p Q3View_StartRendering Failed!");
  138.  
  139.     do
  140.     {
  141.                 /* SET INTERPOLATION STYLE */
  142.  
  143.         myStatus = Q3Style_Submit(gMyStyleObject_Interpolation,gMyViewObject);
  144.         if ( myStatus == kQ3Failure )
  145.             DoFatalAlert("\p Q3Style_Submit Failed!");
  146.             
  147.                 /* SET BACKFACING STYLE */
  148.                     
  149.         myStatus = Q3Style_Submit(gMyStyleObject_Backfacing,gMyViewObject);
  150.         if ( myStatus == kQ3Failure )
  151.             DoFatalAlert("\p Q3Style_Submit Failed!");
  152.             
  153.             
  154.                     /* SET FILL STYLE */
  155.                     
  156.         myStatus = Q3Style_Submit(gMyStyleObject_Fillstyle, gMyViewObject);
  157.         if ( myStatus == kQ3Failure )
  158.             DoFatalAlert("\p Q3Style_Submit Failed!");
  159.             
  160.             
  161.                     /* SET SHADER TO USE */
  162.                     
  163.         myStatus = Q3Shader_Submit(gMyShaderObject_IlluminationShader, gMyViewObject);
  164.         if ( myStatus == kQ3Failure )
  165.             DoFatalAlert("\p Q3Shader_Submit Failed!");
  166.             
  167.             
  168.                 /* DRAW THE POLYGON OBJECT */
  169.                 
  170.         myStatus = Q3DisplayGroup_Submit(gMyPolygonGroup,gMyViewObject);
  171.         if ( myStatus == kQ3Failure )
  172.             DoFatalAlert("\p Drawing Mesh Failed!");
  173.         
  174.         
  175.                     /* SEE IF DONE */
  176.                     
  177.         myViewStatus = Q3View_EndRendering(gMyViewObject);
  178.         
  179.     } while ( myViewStatus == kQ3ViewStatusRetraverse );
  180. }
  181.  
  182.  
  183.  
  184.